#include "PacketTreeModel.h"
#include <QFont>
#include <QtCore>
#include <QModelIndex>
#include <QVariant>
#include <QAbstractItemModel>
#include <QIcon>
#include <QVector>


PacketTreeModel::PacketTreeModel(QObject *parent)
	:QAbstractItemModel(parent)
{
	rootNode = 0;
	icon.push_back(QIcon(tr("Resources/protocal.png")));
	icon.push_back(QIcon(tr("Resources/computer.png")));
	icon.push_back(QIcon(tr("Resources/network_card.png")));
	icon.push_back(QIcon(tr("Resources/field.png")));	
}

PacketTreeModel::~PacketTreeModel(void)
{
}

void PacketTreeModel::setRootNode(ShowableItem *showableitem)
{
	delete rootNode;
	
	rootNode = showableitem;
	reset();
}




QModelIndex PacketTreeModel::index(int row,int column,const QModelIndex &parent) const
{
	if (!rootNode || row < 0 || column < 0)
		return QModelIndex();
	ShowableItem *parentNode = nodeFromIndex(parent);
	const ShowableItem *childNode = parentNode->getChild(row);
	if (!childNode)
		return QModelIndex();
	return createIndex(row, column, (void*)childNode);
}
QModelIndex PacketTreeModel::parent(const QModelIndex &child) const{
	ShowableItem *node = nodeFromIndex(child);
	if (!node)
		return QModelIndex();
	ShowableItem *parentNode = node->getParent();
	if (!parentNode)
		return QModelIndex();
	ShowableItem *grandparentNode = parentNode->getParent();
	if (!grandparentNode)
		return QModelIndex();

	int row = grandparentNode->indexOf(parentNode);
	return createIndex(row, 0, parentNode);
}

int PacketTreeModel::rowCount(const QModelIndex &parent) const{
	if (parent.column() > 0)
		return 0;
	ShowableItem *parentNode = nodeFromIndex(parent);
	if (!parentNode)
		return 0;
	return parentNode->childCount();
}



QVariant PacketTreeModel::data(const QModelIndex &index,int role)const{
	if (role != Qt::DisplayRole&&role !=Qt::DecorationRole&&role != Qt::FontRole)
		return QVariant();

	QString str;
	ShowableItem *node = nodeFromIndex(index);
	QFont temp;

	if (!node)
		return QVariant();

		
	if (index.column() == 0) {
		if (role ==Qt::DecorationRole)
		{
			switch(node->getIconType())
			{
			case ShowableItem::ITEMICON_PROTOCAL:
				return icon.at(0);
			case ShowableItem::ITEMICON_HOST:
				return icon.at(1);
			case ShowableItem::ITEMICON_NETCARD:
				return icon.at(2);
			case ShowableItem::ITEMICON_FIELD:
				return icon.at(3);
			}
		}
		else if (role == Qt::FontRole)
		{
			temp.setFamily("Yahei Mono");
			temp.setPointSize(12);

			return temp;
		}
		else
		{
			str.sprintf("%-30s%s",qPrintable(node->getKey()),qPrintable(node->getValue()));
			return str;
		}
	}
	return QVariant();
}
QVariant PacketTreeModel::headerData(int section,
								  Qt::Orientation orientation,
								  int role) const
{
	if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
		if (section == 0) {
			return tr("ShowableItem");
		} else if (section == 1) {
			return tr("Value");
		}
	}
	return QVariant();
}


ShowableItem *PacketTreeModel::nodeFromIndex(const QModelIndex &index)const{
	if (index.isValid()) {
		return static_cast<ShowableItem *>(index.internalPointer());
	} else {
		return rootNode;
	}
}



QModelIndex PacketTreeModel::getSelectIndex(ShowableItem *item,int Begin,int Length)
{
	ShowableItem *temp;
	QModelIndex tmp;
	temp = item;
	for (int i=0;i<item->childCount();i++)
	{
		if (item->childCount()!=0)
		{
			temp = (ShowableItem *)item->getChild(i);
		}
		if (temp->childCount() != 0)
		{
			tmp = getSelectIndex(temp,Begin,Length);
			if (tmp.isValid())
			{
				return tmp;
			}
			else continue;
		}else
		{
			if (temp->getBeginIndex() == Begin)
			{
				int row = temp->getParent()->indexOf(temp);
				return createIndex(row,0,temp);
			}
		}
	}
	return QModelIndex();
}

void PacketTreeModel::setQHexEditSelection(const QModelIndex &index)
{
	ShowableItem *temp;
	temp = this->getShowableItem(index);

	if (!temp)
	{
		return;
	}
	this->qhe->setSelection(temp->getBeginIndex(),temp->getEndIndex());
}

